﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Microsoft.DirectX;

namespace SGDAnimationEditor
{
    public partial class Form1 : Form
    {
        SGP.ManagedDirect3D d3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager tm = SGP.ManagedTextureManager.Instance;
        private bool isLooping;
        public bool IsLooping { get { return isLooping; } set { isLooping = value; } }
        //List<string> m_lFileList = new List<string>();
        Dictionary<string, int> m_dFileDictonary = new Dictionary<string, int>();
        List<Animation> m_lAnimations = new List<Animation>();
        string m_sProjectPath = "";
        string m_sResourceFolder = "";
        //Bitmap m_bmCurrentBitmap = null;
        int m_nCurrentTexture = -1;
        public Form1()
        {
            IsLooping = true;
            InitializeComponent();
            m_sProjectPath = Application.StartupPath.ToString();
            m_sResourceFolder = "Resource";

            d3D.InitManagedDirect3D(WorkPanel);
            d3D.InitManagedDirect3D(PreviewPanel);
            tm.InitManagedTextureManager(d3D.Device, d3D.Sprite);
            AddFrameButton.Enabled = false;
            RemoveFrameButton.Enabled = false;
            GBAnimationInfo.Enabled = false;
            ShowFrameInformation();
            BPlay.Enabled = true;
            BPause.Enabled = false;
            m_bIsPreview = false;
        }

        float frameDuration = 0;
        float pervTime = DateTime.Now.Millisecond;
        public new void Update()
        {
            float delta = DateTime.Now.Millisecond - pervTime;
            delta /= 1000.0f;//milliconds to seconds
            pervTime = DateTime.Now.Millisecond;

            m_fmFrame = CurrentFrame();
            if (m_bIsPreview)
            {
                if(delta>0)
                    frameDuration -= delta;
                if (frameDuration <= 0)
                {
                    if (FrameList.Items.Count > 0)
                    {
                        if (FrameList.SelectedIndex < FrameList.Items.Count - 1)
                            FrameList.SelectedIndex += 1;
                        else if (FrameList.SelectedIndex == FrameList.Items.Count - 1)
                            FrameList.SelectedIndex = 0;
                        m_fmFrame = CurrentFrame();
                        frameDuration = m_fmFrame.FrameDuration;
                        ShowFrameInformation();

                        if (m_fmFrame.FileName != null)
                        {
                            if(FileList.Items.Contains(m_fmFrame.FileName))
                                FileList.SelectedIndex = FileList.Items.IndexOf(m_fmFrame.FileName);
 
                        }
                    }
                }
            }
        }

        public void Render()
        {
            d3D.ChangeDisplayParam(WorkPanel);
            d3D.Clear(WorkPanel, Color.White);

            d3D.DeviceBegin();
            d3D.SpriteBegin();
            WorkPanelPaint();
            d3D.SpriteEnd();
            WorkPanelPrimitivePaint();
            d3D.DeviceEnd();
            
            d3D.Present();

            d3D.ChangeDisplayParam(PreviewPanel);
            d3D.Clear(PreviewPanel, Color.White);
            
            d3D.DeviceBegin();
            d3D.SpriteBegin();
            PreviewPanelPaint();
            d3D.SpriteEnd();
            PreviewPanelPrimitivePaint();
            d3D.DeviceEnd();
            
            d3D.Present();
        }

        private void WorkPanelPaint()
        {
            Point offset = new Point(0, 0);
            //offset.X += WorkPanel.AutoScrollPosition.X;
            //offset.Y += WorkPanel.AutoScrollPosition.Y;

            if (m_nCurrentTexture != -1)
            {
                tm.Draw(m_nCurrentTexture,offset.X,offset.Y,1.0f,1.0f,Rectangle.Empty,0,0,0,Color.White);
            }
            else
            {
            }
        }

        private void WorkPanelPrimitivePaint()
        {
            if (m_fmFrame != null)
            {
                d3D.DrawEmptyRect(m_fmFrame.RenderRect, Color.Red, 1);
                d3D.DrawEmptyRect(m_fmFrame.ActiveCollisionRect, Color.Orange, 1);
                d3D.DrawEmptyRect(m_fmFrame.PassiveCollisionRect, Color.Green, 1);
                DrawAnchorPoint(m_fmFrame.AnchorX, m_fmFrame.AnchorY);
            }
        }

        private void DrawAnchorPoint(int p1, int p2)
        {
            int size = 5;
            d3D.DrawLine(p1 - size, p2, p1 + size, p2, Color.Blue, 1);
            d3D.DrawLine(p1, p2 - size, p1, p2 + size, Color.Blue, 1);
        }

        private void PreviewPanelPaint()
        {
            if (m_fmFrame != null)
            {
                Point offset = new Point(0, 0);
                offset.X += PreviewPanel.AutoScrollPosition.X;
                offset.Y += PreviewPanel.AutoScrollPosition.Y;

                if (m_nCurrentTexture != -1)
                {
                    int x = m_pPreviewAnchorPoint.X - (m_fmFrame.AnchorX - m_fmFrame.RenderRect.X);
                    int y = m_pPreviewAnchorPoint.Y - (m_fmFrame.AnchorY - m_fmFrame.RenderRect.Y);

                    tm.Draw(m_nCurrentTexture, x, y, 1.0f, 1.0f, m_fmFrame.RenderRect, 0, 0, 0, Color.White);
                }
                else
                {
                }
            }
        }

        private void PreviewPanelPrimitivePaint()
        {
            if (m_fmFrame != null)
            {
                {
                    int x = m_pPreviewAnchorPoint.X - (m_fmFrame.AnchorX - m_fmFrame.RenderRect.X);
                    int y = m_pPreviewAnchorPoint.Y - (m_fmFrame.AnchorY - m_fmFrame.RenderRect.Y);
                    Rectangle rect = new Rectangle(x, y, m_fmFrame.RenderRect.Width, m_fmFrame.RenderRect.Height);
                    d3D.DrawEmptyRect(rect, Color.Red, 1);
                }
                {
                    int x = m_pPreviewAnchorPoint.X - (m_fmFrame.AnchorX - m_fmFrame.ActiveCollisionRect.X);
                    int y = m_pPreviewAnchorPoint.Y - (m_fmFrame.AnchorY - m_fmFrame.ActiveCollisionRect.Y);
                    Rectangle rect = new Rectangle(x, y, m_fmFrame.ActiveCollisionRect.Width, m_fmFrame.ActiveCollisionRect.Height);
                    d3D.DrawEmptyRect(rect, Color.Orange, 1);
                }
                {
                    int x = m_pPreviewAnchorPoint.X - (m_fmFrame.AnchorX - m_fmFrame.PassiveCollisionRect.X);
                    int y = m_pPreviewAnchorPoint.Y - (m_fmFrame.AnchorY - m_fmFrame.PassiveCollisionRect.Y);
                    Rectangle rect = new Rectangle(x, y, m_fmFrame.PassiveCollisionRect.Width, m_fmFrame.PassiveCollisionRect.Height);
                    d3D.DrawEmptyRect(rect, Color.Green, 1);
                }
            }
            DrawAnchorPoint(m_pPreviewAnchorPoint.X, m_pPreviewAnchorPoint.Y);
        }

        private void FileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FileList.SelectedIndex != -1)
            {
                m_nCurrentTexture = m_dFileDictonary[(string)FileList.Items[FileList.SelectedIndex]];
                if (m_fmFrame != null)
                {
                    m_fmFrame.FileName = FileList.Items[FileList.SelectedIndex].ToString();
                    TBFileName.Text = m_fmFrame.FileName;
                }
            }
            else
                m_nCurrentTexture = -1;
        }

        private void AddFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = Path.Combine(m_sProjectPath,m_sResourceFolder);
            if (DialogResult.OK == dlg.ShowDialog())
            {
                string fileName = dlg.SafeFileName;
                int _key;
                if (SetBitmap(Path.Combine(m_sResourceFolder, fileName),out _key))
                {
                    m_dFileDictonary.Add(fileName, _key);
                    FileList.Items.Add(fileName);
                }
            }
        }

        private bool SetBitmap(string filePath, out int _key)
        {
            try
            {
                _key = tm.LoadTexture(filePath, 0);
                return true;
            }
            catch
            {
                _key = -1;
                m_nCurrentTexture = -1;
                return false;
            }
        }

        private void RemoveFileButton_Click(object sender, EventArgs e)
        {
            if (FileList.SelectedIndex != -1)
            {
                m_dFileDictonary.Remove(FileList.Items[FileList.SelectedIndex].ToString());
                FileList.Items.RemoveAt(FileList.SelectedIndex);
            }
        }

        public class Frame
        {
            public Rectangle RenderRect = new Rectangle();
            public Rectangle ActiveCollisionRect = new Rectangle();
            public Rectangle PassiveCollisionRect = new Rectangle();
            public int AnchorX, AnchorY;
            public float FrameDuration;
            public string FrameTrigger;
            public string FileName;
            public Frame()
            {
                FrameDuration = 0.1f;
            }
        }

        Frame m_fmFrame;
        private SelectedPoint m_eSelectedPoint = SelectedPoint.None;
        private bool m_bSelectPoint = false;
        private enum SelectedPoint
        {
            None,
            Left,
            Up,
            Down,
            Right,
        }

        public bool PointWithinTwoPoint(int x, int y, Point pt1, Point pt2)
        {
            int range = 5;
            int w = Math.Min(pt1.X,pt2.X) - range;
            int e = Math.Max(pt1.X,pt2.X) + range;
            int n = Math.Min(pt1.Y,pt2.Y) - range;
            int s = Math.Max(pt1.Y,pt2.Y) + range;
            if (x < w)
                return false;
            if (x > e)
                return false;
            if (y < n)
                return false;
            if (y > s)
                return false;
            return true;
        }

        private bool m_bIsMouseDown = false;
        private Point m_pStartFrameClickPoint = new Point();
        private void WorkPanel_MouseDown(object sender, MouseEventArgs e)
        {
            if (m_bIsPreview)
            {
                ShowFrameInformation();
                return;
            }
            if (m_fmFrame != null)
            {

                if (e.Button == MouseButtons.Left)
                {
                    if (m_bSelectPoint)
                    {
                        if (currentSelectionRect == SelectionRect.Render)
                            CheckPointWithinTwoPoint(e.X, e.Y, m_fmFrame.RenderRect);
                        else if (currentSelectionRect == SelectionRect.Active)
                            CheckPointWithinTwoPoint(e.X, e.Y, m_fmFrame.ActiveCollisionRect);
                        else if (currentSelectionRect == SelectionRect.Passive)
                            CheckPointWithinTwoPoint(e.X, e.Y, m_fmFrame.PassiveCollisionRect);

                        m_pStartFrameClickPoint.X = e.X;
                        m_pStartFrameClickPoint.Y = e.Y;
                        m_bIsMouseDown = true;
                    }
                    else
                    {
                        if (currentSelectionRect == SelectionRect.Render)
                        {
                            m_fmFrame.RenderRect.X = e.X;
                            m_fmFrame.RenderRect.Y = e.Y;
                            m_fmFrame.RenderRect.Width = 0;
                            m_fmFrame.RenderRect.Height = 0;
                        }
                        else if (currentSelectionRect == SelectionRect.Active)
                        {
                            m_fmFrame.ActiveCollisionRect.X = e.X;
                            m_fmFrame.ActiveCollisionRect.Y = e.Y;
                            m_fmFrame.ActiveCollisionRect.Width = 0;
                            m_fmFrame.ActiveCollisionRect.Height = 0;
                        }
                        else if (currentSelectionRect == SelectionRect.Passive)
                        {
                            m_fmFrame.PassiveCollisionRect.X = e.X;
                            m_fmFrame.PassiveCollisionRect.Y = e.Y;
                            m_fmFrame.PassiveCollisionRect.Width = 0;
                            m_fmFrame.PassiveCollisionRect.Height = 0;
                        }
                        m_pStartFrameClickPoint.X = e.X;
                        m_pStartFrameClickPoint.Y = e.Y;
                        m_bIsMouseDown = true;
                    }
                }
                else if (e.Button == MouseButtons.Right)
                {
                    SetFrameAnchorPoint(e);
                }
                ShowFrameInformation();
            }
        }

        private void CheckPointWithinTwoPoint(int x, int y, Rectangle _Rectangle)
        {
            if (PointWithinTwoPoint(x, y,
                new Point(_Rectangle.Left, _Rectangle.Top),
                new Point(_Rectangle.Right, _Rectangle.Top)))
                m_eSelectedPoint = SelectedPoint.Up;
            else if (PointWithinTwoPoint(x, y,
                new Point(_Rectangle.Left, _Rectangle.Top),
                new Point(_Rectangle.Left, _Rectangle.Bottom)))
                m_eSelectedPoint = SelectedPoint.Left;
            else if (PointWithinTwoPoint(x, y,
                new Point(_Rectangle.Left, _Rectangle.Bottom),
                new Point(_Rectangle.Right, _Rectangle.Bottom)))
                m_eSelectedPoint = SelectedPoint.Down;
            else if (PointWithinTwoPoint(x, y,
                new Point(_Rectangle.Right, _Rectangle.Top),
                new Point(_Rectangle.Right, _Rectangle.Bottom)))
                m_eSelectedPoint = SelectedPoint.Right;
            else
                m_eSelectedPoint = SelectedPoint.None;
        }

        private void WorkPanel_MouseUp(object sender, MouseEventArgs e)
        {
            m_eSelectedPoint = SelectedPoint.None;
            if (m_bIsPreview)
            {
                ShowFrameInformation();
                return;
            }
            if (m_fmFrame != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if(!m_bSelectPoint)
                        ChangeFrameRect(e);
                    m_bIsMouseDown = false;
                }
                else if (e.Button == MouseButtons.Right)
                {
                    SetFrameAnchorPoint(e);
                }
                ShowFrameInformation();
            }
        }

        private void WorkPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_bIsPreview)
            {
                ShowFrameInformation();
                return;
            }
            if (m_fmFrame != null)
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (m_bIsMouseDown)
                        ChangeFrameRect(e);
                }
                else if (e.Button == MouseButtons.Right)
                {
                    SetFrameAnchorPoint(e);
                }
                ShowFrameInformation();
            }
        }

        private void SetFrameAnchorPoint(MouseEventArgs e)
        {
            if (m_fmFrame != null)
            {
                Point clickPoint = new Point(e.X, e.Y);
                if (clickPoint.X < 0)
                    clickPoint.X = 0;
                if (clickPoint.Y < 0)
                    clickPoint.Y = 0;
                m_fmFrame.AnchorX = clickPoint.X;
                m_fmFrame.AnchorY = clickPoint.Y;
            }
        }

        private void ChangeFrameRect(MouseEventArgs e)
        {
            if (m_fmFrame != null)
            {
                Point clickPoint = new Point(e.X, e.Y);
                if (clickPoint.X < 0)
                    clickPoint.X = 0;
                if (clickPoint.Y < 0)
                    clickPoint.Y = 0;
                //Point newPosition = new Point();
                //m_fmFrame.RenderRect.Width = Math.Abs(clickPoint.X - m_pStartFrameClickPoint.X);
                //newPosition.X = Math.Min(clickPoint.X, m_pStartFrameClickPoint.X);
                //m_fmFrame.RenderRect.X = newPosition.X;
                //m_fmFrame.RenderRect.Height = Math.Abs(clickPoint.Y - m_pStartFrameClickPoint.Y);
                //newPosition.Y = Math.Min(clickPoint.Y, m_pStartFrameClickPoint.Y);
                //m_fmFrame.RenderRect.Y = newPosition.Y;
                if ((m_eSelectedPoint == SelectedPoint.None)||(m_bSelectPoint == false))
                {
                    if (currentSelectionRect == SelectionRect.Render)
                    {
                        Point newPosition = new Point();
                        m_fmFrame.RenderRect.Width = Math.Abs(clickPoint.X - m_pStartFrameClickPoint.X);
                        newPosition.X = Math.Min(clickPoint.X, m_pStartFrameClickPoint.X);
                        m_fmFrame.RenderRect.X = newPosition.X;
                        m_fmFrame.RenderRect.Height = Math.Abs(clickPoint.Y - m_pStartFrameClickPoint.Y);
                        newPosition.Y = Math.Min(clickPoint.Y, m_pStartFrameClickPoint.Y);
                        m_fmFrame.RenderRect.Y = newPosition.Y;
                    }
                    else if (currentSelectionRect == SelectionRect.Active)
                    {
                        Point newPosition = new Point();
                        m_fmFrame.ActiveCollisionRect.Width = Math.Abs(clickPoint.X - m_pStartFrameClickPoint.X);
                        newPosition.X = Math.Min(clickPoint.X, m_pStartFrameClickPoint.X);
                        m_fmFrame.ActiveCollisionRect.X = newPosition.X;
                        m_fmFrame.ActiveCollisionRect.Height = Math.Abs(clickPoint.Y - m_pStartFrameClickPoint.Y);
                        newPosition.Y = Math.Min(clickPoint.Y, m_pStartFrameClickPoint.Y);
                        m_fmFrame.ActiveCollisionRect.Y = newPosition.Y;
                    }
                    else if (currentSelectionRect == SelectionRect.Passive)
                    {
                        Point newPosition = new Point();
                        m_fmFrame.PassiveCollisionRect.Width = Math.Abs(clickPoint.X - m_pStartFrameClickPoint.X);
                        newPosition.X = Math.Min(clickPoint.X, m_pStartFrameClickPoint.X);
                        m_fmFrame.PassiveCollisionRect.X = newPosition.X;
                        m_fmFrame.PassiveCollisionRect.Height = Math.Abs(clickPoint.Y - m_pStartFrameClickPoint.Y);
                        newPosition.Y = Math.Min(clickPoint.Y, m_pStartFrameClickPoint.Y);
                        m_fmFrame.PassiveCollisionRect.Y = newPosition.Y;
                    }
                }
                else
                {
                    DetailChangeFrameRect(e);
                    
                }
            }
        }

        private void DetailChangeFrameRect(MouseEventArgs e)
        {
            if (currentSelectionRect == SelectionRect.Render)
            {
                if (m_eSelectedPoint == SelectedPoint.Left)
                {
                    if (m_fmFrame.RenderRect.Width - (e.X - m_fmFrame.RenderRect.X) > 0)
                    {
                        m_fmFrame.RenderRect.Width -= e.X - m_fmFrame.RenderRect.X;
                        m_fmFrame.RenderRect.X = e.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Right)
                {
                    if (e.X - m_fmFrame.RenderRect.X > 0)
                    {
                        m_fmFrame.RenderRect.Width = e.X - m_fmFrame.RenderRect.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Up)
                {
                    if (m_fmFrame.RenderRect.Height - (e.Y - m_fmFrame.RenderRect.Y) > 0)
                    {
                        m_fmFrame.RenderRect.Height -= e.Y - m_fmFrame.RenderRect.Y;
                        m_fmFrame.RenderRect.Y = e.Y;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Down)
                {
                    if (e.Y - m_fmFrame.RenderRect.Y > 0)
                    {
                        m_fmFrame.RenderRect.Height = e.Y - m_fmFrame.RenderRect.Y;
                    }
                }
            }
            else if (currentSelectionRect == SelectionRect.Active)
            {
                if (m_eSelectedPoint == SelectedPoint.Left)
                {
                    if (m_fmFrame.ActiveCollisionRect.Width - (e.X - m_fmFrame.ActiveCollisionRect.X) > 0)
                    {
                        m_fmFrame.ActiveCollisionRect.Width -= e.X - m_fmFrame.ActiveCollisionRect.X;
                        m_fmFrame.ActiveCollisionRect.X = e.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Right)
                {
                    if (e.X - m_fmFrame.ActiveCollisionRect.X > 0)
                    {
                        m_fmFrame.ActiveCollisionRect.Width = e.X - m_fmFrame.ActiveCollisionRect.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Up)
                {
                    if (m_fmFrame.ActiveCollisionRect.Height - (e.Y - m_fmFrame.ActiveCollisionRect.Y) > 0)
                    {
                        m_fmFrame.ActiveCollisionRect.Height -= e.Y - m_fmFrame.ActiveCollisionRect.Y;
                        m_fmFrame.ActiveCollisionRect.Y = e.Y;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Down)
                {
                    if (e.Y - m_fmFrame.ActiveCollisionRect.Y > 0)
                    {
                        m_fmFrame.ActiveCollisionRect.Height = e.Y - m_fmFrame.ActiveCollisionRect.Y;
                    }
                }
            }
            else if(currentSelectionRect == SelectionRect.Passive)
            {
                if (m_eSelectedPoint == SelectedPoint.Left)
                {
                    if (m_fmFrame.PassiveCollisionRect.Width - (e.X - m_fmFrame.PassiveCollisionRect.X) > 0)
                    {
                        m_fmFrame.PassiveCollisionRect.Width -= e.X - m_fmFrame.PassiveCollisionRect.X;
                        m_fmFrame.PassiveCollisionRect.X = e.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Right)
                {
                    if (e.X - m_fmFrame.PassiveCollisionRect.X > 0)
                    {
                        m_fmFrame.PassiveCollisionRect.Width = e.X - m_fmFrame.PassiveCollisionRect.X;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Up)
                {
                    if (m_fmFrame.PassiveCollisionRect.Height - (e.Y - m_fmFrame.PassiveCollisionRect.Y) > 0)
                    {
                        m_fmFrame.PassiveCollisionRect.Height -= e.Y - m_fmFrame.PassiveCollisionRect.Y;
                        m_fmFrame.PassiveCollisionRect.Y = e.Y;
                    }
                }
                if (m_eSelectedPoint == SelectedPoint.Down)
                {
                    if (e.Y - m_fmFrame.PassiveCollisionRect.Y > 0)
                    {
                        m_fmFrame.PassiveCollisionRect.Height = e.Y - m_fmFrame.PassiveCollisionRect.Y;
                    }
                }
            }
        }

        bool m_bIsPreviewMouseDown = false;
        Point m_pPreviewAnchorPoint = new Point();
        private void PreviewPanel_MouseDown(object sender, MouseEventArgs e)
        {
            m_bIsPreviewMouseDown = true;
            m_pPreviewAnchorPoint.X = e.X;
            m_pPreviewAnchorPoint.Y = e.Y;
        }

        private void PreviewPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_bIsPreviewMouseDown)
            {
                m_pPreviewAnchorPoint.X = e.X;
                m_pPreviewAnchorPoint.Y = e.Y;
            }
        }

        private void PreviewPanel_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_bIsPreviewMouseDown)
            {
                m_pPreviewAnchorPoint.X = e.X;
                m_pPreviewAnchorPoint.Y = e.Y;
            }
            m_bIsPreviewMouseDown = false;

        }

        private void AddAnimationButton_Click(object sender, EventArgs e)
        {
            Animation animation = new Animation();
            animation.Name = ""+AnimationList.Items.Count;
            m_lAnimations.Add(animation);
            RefreshAnimationList();
            RefreshFrameList(animation);
            FrameList.Items.Clear();
            FrameList.SelectedIndex = -1;
            m_fmFrame = null;
            ShowAnimationInfo();
            RefreshFileList();
            ShowFrameInformation();
        }

        private void RefreshAnimationList()
        {
            AnimationList.SelectedIndex = -1;
            AnimationList.Items.Clear();
            for (int i = 0; i < m_lAnimations.Count; ++i)
                AnimationList.Items.Add(m_lAnimations[i].Name);
        }

        private void RefreshFrameList(Animation animation)
        {
            FrameList.SelectedIndex = -1;
            FrameList.Items.Clear();
            for (int i = 0; i < animation.m_lFrameList.Count; ++i)
                FrameList.Items.Add(i);
        }

        private void RemoveAnimationButton_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                m_lAnimations.RemoveAt(AnimationList.SelectedIndex);
                AnimationList.Items.RemoveAt(AnimationList.SelectedIndex);
                AnimationList.SelectedIndex = -1;
            }
            FrameList.Items.Clear();
            FrameList.SelectedIndex = -1;
            m_fmFrame = null;
            ShowAnimationInfo();
            RefreshFileList();
            ShowFrameInformation();
        }

        private void ShowAnimationInfo()
        {
            if (AnimationList.SelectedIndex != -1)
            {
                GBAnimationInfo.Enabled = true;
                TBAnimationName.Text = m_lAnimations[AnimationList.SelectedIndex].Name;
                CBLoop.Checked = m_lAnimations[AnimationList.SelectedIndex].m_bIsLooping;
            }
            else
            {
                GBAnimationInfo.Enabled = false;
                TBAnimationName.Text = "";
                CBLoop.Checked = false;
            }
        }

        public Frame CurrentFrame()
        {
            if (AnimationList.SelectedIndex != -1)
                if (FrameList.SelectedIndex != -1)
                    if (AnimationList.SelectedIndex < m_lAnimations.Count)
                        if (FrameList.SelectedIndex < m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count)
                    return m_lAnimations[AnimationList.SelectedIndex].m_lFrameList[FrameList.SelectedIndex];
            return null;
        }

        private void AddFrameButton_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
                m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Add(new Frame());
            RefreshFrameList(m_lAnimations[AnimationList.SelectedIndex]);
            m_fmFrame = null;
            RefreshFileList();
            ShowFrameInformation();
        }

        private void RefreshFileList()
        {
            if (m_fmFrame != null)
                if (m_fmFrame.FileName != null)
                    if (FileList.Items.Contains(m_fmFrame.FileName))
                    {
                        FileList.SelectedIndex = FileList.Items.IndexOf(m_fmFrame.FileName);
                        return;
                    }
            FileList.SelectedIndex = -1;
        }

        private void RemoveFrameButton_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
                if (FrameList.SelectedIndex != -1)
                    if (m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count > 0)
                        m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.RemoveAt(FrameList.SelectedIndex);
            RefreshFrameList(m_lAnimations[AnimationList.SelectedIndex]);
            m_fmFrame = null;
            RefreshFileList();
            ShowFrameInformation();
        }

        public class Animation
        {
            public List<Frame> m_lFrameList = new List<Frame>();
            private string m_sName;
            public bool m_bIsLooping;
            public string Name{get{return m_sName;}set{m_sName = value;}}
            public Animation()
            {
                Name = "";
                m_bIsLooping = false;
            }
        }

        private void PopulateFrameListBox(List<Frame> lFrameList)
        {
            for (int i = 0; i < lFrameList.Count; ++i)
            {
                FrameList.Items.Add(lFrameList[i]);
            }
        }
        
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isLooping = false;
        }

        private void WorkPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void AnimationList_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddFrameButton.Enabled = (AnimationList.SelectedIndex != -1);
            RemoveFrameButton.Enabled = (AnimationList.SelectedIndex != -1);
            if (AnimationList.SelectedIndex != -1)
            {
                RefreshFrameList(m_lAnimations[AnimationList.SelectedIndex]);
                TBAnimationName.Text = m_lAnimations[AnimationList.SelectedIndex].Name;
                RefreshFileList();
                ShowAnimationInfo();
                GBFrameInfo.Enabled = false;
            }
        }

        private void FrameList_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshFileList();
            ShowFrameInformation();
        }

        private void ShowFrameInformation()
        {
            if (m_fmFrame!= null)
            {
                GBFrameInfo.Enabled = !m_bIsPreview;
                NUDAncX.Value = m_fmFrame.AnchorX;
                NUDAncY.Value = m_fmFrame.AnchorY;

                NUDRenX.Value = m_fmFrame.RenderRect.X;
                NUDRenY.Value = m_fmFrame.RenderRect.Y;
                NUDRenW.Value = m_fmFrame.RenderRect.Width;
                NUDRenH.Value = m_fmFrame.RenderRect.Height;

                NUDActX.Value = m_fmFrame.ActiveCollisionRect.X;
                NUDActY.Value = m_fmFrame.ActiveCollisionRect.Y;
                NUDActW.Value = m_fmFrame.ActiveCollisionRect.Width;
                NUDActH.Value = m_fmFrame.ActiveCollisionRect.Height;

                NUDPasX.Value = m_fmFrame.PassiveCollisionRect.X;
                NUDPasY.Value = m_fmFrame.PassiveCollisionRect.Y;
                NUDPasW.Value = m_fmFrame.PassiveCollisionRect.Width;
                NUDPasH.Value = m_fmFrame.PassiveCollisionRect.Height;

                NUDDur.Value = (decimal)m_fmFrame.FrameDuration;
                TBEventTrigger.Text = m_fmFrame.FrameTrigger;
                TBFileName.Text = m_fmFrame.FileName;


            }
            else
            {
                GBFrameInfo.Enabled = false;
                NUDAncX.Value = 0;
                NUDAncY.Value = 0;
                                
                NUDRenX.Value = 0;
                NUDRenY.Value = 0;
                NUDRenW.Value = 0;
                NUDRenH.Value = 0;
                               
                NUDActX.Value = 0;
                NUDActY.Value = 0;
                NUDActW.Value = 0;
                NUDActH.Value = 0;
                                
                NUDPasX.Value = 0;
                NUDPasY.Value = 0;
                NUDPasW.Value = 0;
                NUDPasH.Value = 0;

                NUDDur.Value = 0;
                TBEventTrigger.Text = "";
                TBFileName.Text = "";
            }
        }

        private void NUDAncX_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.AnchorX = (int)NUDAncX.Value;
        }

        private void NUDAncY_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.AnchorY = (int)NUDAncY.Value;
        }

        private void NUDRenX_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.RenderRect.X - (int)NUDRenX.Value;
                if (m_fmFrame.RenderRect.Width >  - change)
                {
                    m_fmFrame.RenderRect.X = (int)NUDRenX.Value;
                    m_fmFrame.RenderRect.Width += change;
                    NUDRenW.Value = m_fmFrame.RenderRect.Width;
                }
                else
                {
                    NUDRenX.Value = m_fmFrame.RenderRect.X;
                }
            }
        }

        private void NUDRenY_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.RenderRect.Y - (int)NUDRenY.Value;
                if (m_fmFrame.RenderRect.Height > -change)
                {
                    m_fmFrame.RenderRect.Y = (int)NUDRenY.Value;
                    m_fmFrame.RenderRect.Height += change;
                    NUDRenH.Value = m_fmFrame.RenderRect.Height;
                }
                else
                {
                    NUDRenY.Value = m_fmFrame.RenderRect.Y;
                }
            }
        }

        private void NUDRenW_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.RenderRect.Width = (int)NUDRenW.Value;
        }

        private void NUDRenH_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.RenderRect.Height = (int)NUDRenH.Value;
        }

        private void NUDDur_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.FrameDuration = (float)NUDDur.Value;
        }

        private void TBEventTrigger_TextChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.FrameTrigger = TBEventTrigger.Text;
        }

        private void NUDActX_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.ActiveCollisionRect.X - (int)NUDActX.Value;
                if (m_fmFrame.ActiveCollisionRect.Width > -change)
                {
                    m_fmFrame.ActiveCollisionRect.X = (int)NUDActX.Value;
                    m_fmFrame.ActiveCollisionRect.Width += change;
                    NUDActW.Value = m_fmFrame.ActiveCollisionRect.Width;
                }
                else
                {
                    NUDActX.Value = m_fmFrame.ActiveCollisionRect.X;
                }
            }
        }

        private void NUDActY_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.ActiveCollisionRect.Y - (int)NUDActY.Value;
                if (m_fmFrame.ActiveCollisionRect.Height > -change)
                {
                    m_fmFrame.ActiveCollisionRect.Y = (int)NUDActY.Value;
                    m_fmFrame.ActiveCollisionRect.Height += change;
                    NUDActH.Value = m_fmFrame.ActiveCollisionRect.Height;
                }
                else
                {
                    NUDActY.Value = m_fmFrame.ActiveCollisionRect.Y;
                }
            }
        }

        private void NUDActW_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.ActiveCollisionRect.Width = (int)NUDActW.Value;
        }

        private void NUDActH_ValueChanged(object sender, EventArgs e)
        {

            if (m_fmFrame != null)
                m_fmFrame.ActiveCollisionRect.Height = (int)NUDActH.Value;
        }

        private void NUDPasX_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.PassiveCollisionRect.X - (int)NUDPasX.Value;
                if (m_fmFrame.PassiveCollisionRect.Width > -change)
                {
                    m_fmFrame.PassiveCollisionRect.X = (int)NUDPasX.Value;
                    m_fmFrame.PassiveCollisionRect.Width += change;
                    NUDPasW.Value = m_fmFrame.PassiveCollisionRect.Width;
                }
                else
                {
                    NUDPasX.Value = m_fmFrame.PassiveCollisionRect.X;
                }
            }
        }

        private void NUDPasY_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
            {
                int change = m_fmFrame.PassiveCollisionRect.Y - (int)NUDPasY.Value;
                if (m_fmFrame.PassiveCollisionRect.Height > -change)
                {
                    m_fmFrame.PassiveCollisionRect.Y = (int)NUDPasY.Value;
                    m_fmFrame.PassiveCollisionRect.Height += change;
                    NUDPasH.Value = m_fmFrame.PassiveCollisionRect.Height;
                }
                else
                {
                    NUDPasY.Value = m_fmFrame.PassiveCollisionRect.Y;
                }
            }
        }

        private void NUDPasW_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.PassiveCollisionRect.Width = (int)NUDPasW.Value;
        }

        private void NUDPasH_ValueChanged(object sender, EventArgs e)
        {
            if (m_fmFrame != null)
                m_fmFrame.PassiveCollisionRect.Height = (int)NUDPasH.Value;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = m_sProjectPath;//Path.Combine(m_sProjectPath, m_sResourceFolder);
            
            string fileName;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                fileName = dlg.FileName;
            }
            else
                return;
            XmlTextWriter write = new XmlTextWriter(fileName, Encoding.UTF8);
            write.Formatting = Formatting.Indented;
            
            write.WriteStartDocument();
            write.WriteStartElement("Root");
            {
                write.WriteStartElement("Files");
                foreach (string key in m_dFileDictonary.Keys)
                {
                    write.WriteStartElement("File");
                    write.WriteAttributeString("FileName", key);
                    write.WriteEndElement();//File
                }
                write.WriteFullEndElement();//Files
                {
                    write.WriteStartElement("Animations");
                    for (int i = 0; i < m_lAnimations.Count; ++i)
                    {
                        write.WriteStartElement("Animation");
                        write.WriteAttributeString("Name", m_lAnimations[i].Name);
                        write.WriteAttributeString("IsLooping", m_lAnimations[i].m_bIsLooping ? "1" : "0");
                        {
                            write.WriteStartElement("Frames");
                            for (int j = 0; j < m_lAnimations[i].m_lFrameList.Count; ++j)
                            {
                                write.WriteStartElement("Frame");
                                write.WriteAttributeString("FileName", m_lAnimations[i].m_lFrameList[j].FileName);
                                write.WriteAttributeString("FrameDuration", "" + m_lAnimations[i].m_lFrameList[j].FrameDuration);
                                write.WriteAttributeString("FrameTrigger", m_lAnimations[i].m_lFrameList[j].FrameTrigger);
                                {
                                    write.WriteStartElement("AnchorPoint");
                                    write.WriteAttributeString("x", "" + m_lAnimations[i].m_lFrameList[j].AnchorX);
                                    write.WriteAttributeString("y", "" + m_lAnimations[i].m_lFrameList[j].AnchorY);
                                    write.WriteEndElement();//AnchorPoint
                                }
                                {
                                    write.WriteStartElement("RenderRect");
                                    write.WriteAttributeString("x", "" + m_lAnimations[i].m_lFrameList[j].RenderRect.X);
                                    write.WriteAttributeString("y", "" + m_lAnimations[i].m_lFrameList[j].RenderRect.Y);
                                    write.WriteAttributeString("width", "" + m_lAnimations[i].m_lFrameList[j].RenderRect.Width);
                                    write.WriteAttributeString("height", "" + m_lAnimations[i].m_lFrameList[j].RenderRect.Height);
                                    write.WriteEndElement();//RenderRect
                                }
                                {
                                    write.WriteStartElement("ActiveCollisionRect");
                                    write.WriteAttributeString("x", "" + m_lAnimations[i].m_lFrameList[j].ActiveCollisionRect.X);
                                    write.WriteAttributeString("y", "" + m_lAnimations[i].m_lFrameList[j].ActiveCollisionRect.Y);
                                    write.WriteAttributeString("width", "" + m_lAnimations[i].m_lFrameList[j].ActiveCollisionRect.Width);
                                    write.WriteAttributeString("height", "" + m_lAnimations[i].m_lFrameList[j].ActiveCollisionRect.Height);
                                    write.WriteEndElement();//ActiveCollisionRect
                                }
                                {
                                    write.WriteStartElement("PassiveCollisionRect");
                                    write.WriteAttributeString("x", "" + m_lAnimations[i].m_lFrameList[j].PassiveCollisionRect.X);
                                    write.WriteAttributeString("y", "" + m_lAnimations[i].m_lFrameList[j].PassiveCollisionRect.Y);
                                    write.WriteAttributeString("width", "" + m_lAnimations[i].m_lFrameList[j].PassiveCollisionRect.Width);
                                    write.WriteAttributeString("height", "" + m_lAnimations[i].m_lFrameList[j].PassiveCollisionRect.Height);
                                    write.WriteEndElement();//PassiveCollisionRect
                                }
                                write.WriteFullEndElement();//Frame
                            }
                            write.WriteFullEndElement();//Frames
                        }
                        write.WriteFullEndElement();//Animation
                    }
                    write.WriteFullEndElement();//Animations
                }
                write.WriteFullEndElement();//Root
            }
            write.Close();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = m_sProjectPath;//Path.Combine(m_sProjectPath, m_sResourceFolder);
            string sFileName;
            string relFilePath;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                sFileName = dlg.SafeFileName;
                relFilePath = dlg.FileName.Replace(Directory.GetCurrentDirectory(), sFileName);
                relFilePath = dlg.FileName;
                relFilePath = dlg.FileName;
                relFilePath = dlg.FileName;
            }
            else
                return;

            m_lAnimations.Clear();
            m_dFileDictonary.Clear();
            m_fmFrame = null;
            FileList.Items.Clear();
            FileList.SelectedIndex = -1;
            AnimationList.Items.Clear();
            AnimationList.SelectedIndex = -1;
            FrameList.Items.Clear();
            FrameList.SelectedIndex = -1;

            XmlTextReader r = new XmlTextReader(relFilePath);
            r.WhitespaceHandling = WhitespaceHandling.Significant;
            
            r.Read();//Discard: ?xml
            r.Read();//Discard: Root
            while(r.Read())//Files Animations
            {
                switch (r.Name)
                {
                    case "Files":
                        while (r.Read())
                        {
                            if (r.Name == "Files" && r.NodeType == XmlNodeType.EndElement) break;
                            if (r.NodeType == XmlNodeType.EndElement) continue;

                            string fileName = r.GetAttribute("FileName");
                            int id = tm.LoadTexture( Path.Combine( m_sResourceFolder, fileName), 0);
                            if (id != -1)
                            {
                                m_dFileDictonary.Add(fileName, id);
                                FileList.Items.Add(fileName);
                            }
                        }
                        break;
                    case "Animations":
                        do
                        {
                            if (r.Name == "Animations" && r.NodeType == XmlNodeType.EndElement) break;
                            if (r.NodeType == XmlNodeType.EndElement) continue;

                            while (r.Read())
                            {
                                //if (r.Name == "Animation" && r.NodeType == XmlNodeType.EndElement) break;
                                if (r.NodeType == XmlNodeType.EndElement) continue;

                                Animation a = new Animation();

                                if (r.Name == "Animation")
                                {
                                    a.Name = r.GetAttribute("Name");
                                    a.m_bIsLooping = (r.GetAttribute("IsLooping") == "1");
                                }
                                while (r.Read())
                                {
                                    if (r.Name == "Frames" && r.NodeType == XmlNodeType.EndElement) break;
                                    if (r.NodeType == XmlNodeType.EndElement) continue;

                                    Frame f = new Frame();

                                    if (r.Name == "Frame")
                                    {
                                        f.FrameDuration = float.Parse(r.GetAttribute("FrameDuration"));
                                        f.FrameTrigger = r.GetAttribute("FrameTrigger");
                                        f.FileName = r.GetAttribute("FileName");
                                    }

                                    while (r.Read())
                                    {
                                        if (r.Name == "Frame" && r.NodeType == XmlNodeType.EndElement) break;
                                        if (r.NodeType == XmlNodeType.EndElement) continue;

                                        if (r.Name == "Frame")
                                        {
                                            f.FrameDuration = float.Parse(r.GetAttribute("FrameDuration"));
                                            f.FrameTrigger = r.GetAttribute("FrameTrigger");
                                            f.FileName = r.GetAttribute("FileName");
                                        }

                                        switch (r.Name)
                                        {
                                            case "AnchorPoint":
                                                f.AnchorX = int.Parse(r.GetAttribute("x"));
                                                f.AnchorY = int.Parse(r.GetAttribute("y"));
                                                break;
                                            case "RenderRect":
                                                f.RenderRect.X = int.Parse(r.GetAttribute("x"));
                                                f.RenderRect.Y = int.Parse(r.GetAttribute("y"));
                                                f.RenderRect.Width = int.Parse(r.GetAttribute("width"));
                                                f.RenderRect.Height = int.Parse(r.GetAttribute("height"));
                                                break;
                                            case "ActiveCollisionRect":
                                                f.ActiveCollisionRect.X = int.Parse(r.GetAttribute("x"));
                                                f.ActiveCollisionRect.Y = int.Parse(r.GetAttribute("y"));
                                                f.ActiveCollisionRect.Width = int.Parse(r.GetAttribute("width"));
                                                f.ActiveCollisionRect.Height = int.Parse(r.GetAttribute("height"));
                                                break;
                                            case "PassiveCollisionRect":
                                                f.PassiveCollisionRect.X = int.Parse(r.GetAttribute("x"));
                                                f.PassiveCollisionRect.Y = int.Parse(r.GetAttribute("y"));
                                                f.PassiveCollisionRect.Width = int.Parse(r.GetAttribute("width"));
                                                f.PassiveCollisionRect.Height = int.Parse(r.GetAttribute("height"));
                                                break;
                                        }
                                    }
                                    a.m_lFrameList.Add(f);
                                }
                                m_lAnimations.Add(a);
                            }
                        } while (r.Read());
                        break;
                }
            }
            r.Close();   
            RefreshAnimationList();
        }

        private void TBAnimationName_TextChanged(object sender, EventArgs e)
        {
            int currentIndex = AnimationList.SelectedIndex;
            if (currentIndex != -1)
                m_lAnimations[AnimationList.SelectedIndex].Name = TBAnimationName.Text;
            RefreshAnimationList();
            AnimationList.SelectedIndex = currentIndex;
        }

        public SelectionRect currentSelectionRect = SelectionRect.Render;
        public enum SelectionRect
        {
            Render,
            Active,
            Passive
        }

        private void RBRender_Click(object sender, EventArgs e)
        {
            RBRender.Checked = true;
            RBActive.Checked = false;
            RBPassive.Checked = false;
            currentSelectionRect = SelectionRect.Render;
        }

        private void RBActive_Click(object sender, EventArgs e)
        {
            RBRender.Checked = false;
            RBActive.Checked = true;
            RBPassive.Checked = false;
            currentSelectionRect = SelectionRect.Active;
        }

        private void RBPassive_Click(object sender, EventArgs e)
        {
            RBRender.Checked = false;
            RBActive.Checked = false;
            RBPassive.Checked = true;
            currentSelectionRect = SelectionRect.Passive;
        }

        private void CBLoop_CheckedChanged(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                m_lAnimations[AnimationList.SelectedIndex].m_bIsLooping = CBLoop.Checked;
            }
        }

        private void TBFileName_Click(object sender, EventArgs e)
        {
            if (FileList.SelectedIndex != -1)
                m_fmFrame.FileName = FileList.Items[FileList.SelectedIndex].ToString();

        }

        bool m_bIsPreview = false;
        bool pervStateGBAnimationInfo = false;
        bool pervStateGBFrameInfo = false;

        private void BPlay_Click(object sender, EventArgs e)
        {
            BPlay.Enabled = false;
            BPause.Enabled = true;
            m_bIsPreview = true;
            pervStateGBAnimationInfo = GBAnimationInfo.Enabled;
            pervStateGBFrameInfo = GBFrameInfo.Enabled;
            GBAnimationInfo.Enabled = false;
            GBFrameInfo.Enabled = false;
            AnimationList.Enabled = false;
            FrameList.Enabled = false;
            FileList.Enabled = false;
            AddFileButton.Enabled = false;
            RemoveFileButton.Enabled = false;
            AddAnimationButton.Enabled = false;
            RemoveAnimationButton.Enabled = false;
            AddFrameButton.Enabled = false;
            RemoveFrameButton.Enabled = false;

        }

        private void BPause_Click(object sender, EventArgs e)
        {
            BPlay.Enabled = true;
            BPause.Enabled = false;
            m_bIsPreview = false;
            GBAnimationInfo.Enabled = pervStateGBAnimationInfo;
            GBFrameInfo.Enabled = pervStateGBFrameInfo;
            if (AnimationList.SelectedIndex != -1)
                GBAnimationInfo.Enabled = true;
            if (FrameList.SelectedIndex != -1)
                GBFrameInfo.Enabled = true;
            AnimationList.Enabled = true;
            FrameList.Enabled = true;
            FileList.Enabled = true;
            AddFileButton.Enabled = true;
            RemoveFileButton.Enabled = true;
            AddAnimationButton.Enabled = true;
            RemoveAnimationButton.Enabled = true;
            AddFrameButton.Enabled = true;
            RemoveFrameButton.Enabled = true;
        }

        private void CDDetailMouse_CheckedChanged(object sender, EventArgs e)
        {
            m_bSelectPoint = CDDetailMouse.Checked;
        }

        private void BNextFrame_Click(object sender, EventArgs e)
        {
            if (FrameList.Items.Count > 0)
            {
                if (FrameList.SelectedIndex < FrameList.Items.Count - 1)
                    FrameList.SelectedIndex += 1;
                else if (FrameList.SelectedIndex == FrameList.Items.Count - 1)
                    FrameList.SelectedIndex = 0;
                m_fmFrame = CurrentFrame();
                frameDuration = m_fmFrame.FrameDuration;
                ShowFrameInformation();

                if (m_fmFrame.FileName != null)
                {
                    if (FileList.Items.Contains(m_fmFrame.FileName))
                        FileList.SelectedIndex = FileList.Items.IndexOf(m_fmFrame.FileName);

                }
            }
        }

        private void BBackFrame_Click(object sender, EventArgs e)
        {
            if (FrameList.Items.Count > 0)
            {
                if (FrameList.SelectedIndex > 0)
                    FrameList.SelectedIndex -= 1;
                else if (FrameList.SelectedIndex <= 0)
                    FrameList.SelectedIndex = FrameList.Items.Count - 1;
                m_fmFrame = CurrentFrame();
                frameDuration = m_fmFrame.FrameDuration;
                ShowFrameInformation();

                if (m_fmFrame.FileName != null)
                {
                    if (FileList.Items.Contains(m_fmFrame.FileName))
                        FileList.SelectedIndex = FileList.Items.IndexOf(m_fmFrame.FileName);

                }
            }
        }

        private void WorkPanel_Paint_1(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                for (int i = 0; i < m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count; ++i)
                {
                    m_lAnimations[AnimationList.SelectedIndex].m_lFrameList[i].AnchorY-=(int)NUDAnchor.Value;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                for (int i = 0; i < m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count; ++i)
                {
                    m_lAnimations[AnimationList.SelectedIndex].m_lFrameList[i].AnchorY += (int)NUDAnchor.Value;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (AnimationList.SelectedIndex != -1)
            {
                for (int i = 0; i < m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count; ++i)
                {
                    m_lAnimations[AnimationList.SelectedIndex].m_lFrameList[i].AnchorX -= (int)NUDAnchor.Value;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

            if (AnimationList.SelectedIndex != -1)
            {
                for (int i = 0; i < m_lAnimations[AnimationList.SelectedIndex].m_lFrameList.Count; ++i)
                {
                    m_lAnimations[AnimationList.SelectedIndex].m_lFrameList[i].AnchorX += (int)NUDAnchor.Value;
                }
            }
        }
    }
}
